// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "lawyer_name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "律师名称",
    "label": "律师名称"
  },
  "license_number": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "执业证号",
    "label": "执业证号"
  },
  "email": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "pattern": "^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$"
      }
    ],
    "title": "邮箱",
    "label": "邮箱"
  },
  "law_firm": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "执业律所",
    "label": "执业律所"
  },
  "contact_phone": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "pattern": "^\\+?[0-9\\-]{7,15}$"
      }
    ],
    "title": "联系电话",
    "label": "联系电话"
  },
  "contact_address": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "联系地址",
    "label": "联系地址"
  },
  "legal_specialty": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "法律专长",
    "label": "法律专长"
  },
  "practice_slogan": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "执业感言",
    "label": "执业感言"
  },
  "douyin_account": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "抖音号",
    "label": "抖音号"
  },
  "lawyer_intro": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "律师简介",
    "label": "律师简介"
  },
  "practice_region": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "执业地区",
    "label": "执业地区"
  },
  "personal_photo": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "title": "个人照片",
    "label": "个人照片"
  },
  "license_photo": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "title": "执业证照片",
    "label": "执业证照片"
  },
  "showcase_photos": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "title": "风采照片",
    "label": "风采照片"
  },
  "is_featured": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否推荐页显示",
    "label": "是否推荐页显示"
  }
}

const enumConverter = {}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }